﻿using NCP.Model;
using NCP.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Filters;
using NCP.Core.Extensions;
using NCP.Core.Exceptions;
using System.Data;
using NCP.Util;

namespace WebApp.Controllers
{
    [Authen]
    public class GroupPurchaseController : Controller
    {
        private GroupPurchaseService _service;
        private GroupChoseDrugCompareService _CompareService;
        public GroupPurchaseController(GroupPurchaseService service,GroupChoseDrugCompareService CompareService)
        {
            _service = service;
            _CompareService = CompareService;
        }
    
        public ActionResult Index()
        {
            List<GroupPurchase> list = _CompareService.GroupPurchaseSelect();
            ViewData["gplist"] = list;
            return View();
        }
        public ActionResult DrugCompare(string gpCode)
        {
            GroupPurchase item = _service.GetGroupPurchase(gpCode);
            ViewData["data"] = _service.DrugCompareGetAllBaseInfo();
            ViewData["checkeddata"] = _service.DrugCompareGetGroupDrugInfo(gpCode);
            return View(item);
        }

        public ActionResult DrugTask(string gpCode)
        {
            GroupPurchase item = _service.GetGroupPurchase(gpCode);
            ViewData["data"] = _service.DrugTaskBaseInfo(gpCode);
            return View(item);
        }


        public ActionResult Edit(string gpCode)
        {
            GroupPurchase item;
            if (string.IsNullOrEmpty(gpCode))
            {
                DateTime now = DateTime.Now;
                item = new GroupPurchase();
                item.Code = _service.GetNewGPCode();
                item.Year = now.Year;
                item.BeginDate = now.Date;
                item.EndDate = new DateTime(now.Year, 12, 31, 23, 59, 59);
                ViewData["type"] = "add";
            }
            else
            {
                item = _service.GetGroupPurchase(gpCode);
                ViewData["type"] = "edit";
            }
            return View(item);
        }

        public ActionResult GetGPList()
        {
            Expressionable<GroupPurchase> exp = new Expressionable<GroupPurchase>();
            //string code = Request.Form["code"];
           // string name = Request.Form["name"];
           // int? year = Request.Form["year"].AsInt();
           string gpCode= Request.Form["GroupPurchase"];
            string setpc = Request["setpc"];
            int pageIndex = Request.Form["page"].AsInt(1);
            int pageSize = Request.Form["limit"].AsInt(20);
           // if (!string.IsNullOrEmpty(code)) exp.And(x => x.Code.Contains(code));
           // if (!string.IsNullOrEmpty(name)) exp.And(x => x.Name.Contains(name));
           // if (year.HasValue && year > 0) exp.And(x => x.Year == year);
           if(gpCode!="ALL" && !string.IsNullOrEmpty(gpCode)) exp.And(x => x.Code == gpCode);
            LayData ht = new LayData("0", "获取成功");
            try
            {
                PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
                ht.data = _service.GetGroupPurchasePageList(setpc,exp, pageModel);
                ht.count = pageModel.TotalCount;
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }
        public ActionResult GetGPListExport()
        {
            DataTable dt = _service.GetGPListExport();
            string filename = ExcelHelper.ExportToExcel(dt, "集采批次信息");
            return File(filename, "application/vnd.ms-excel", "集采批次信息.xls");
        }

        public ActionResult GroupDrug(string gpCode)
        {
            GroupPurchase item = _service.GetGroupPurchase(gpCode);
            return View(item);
        }

        public ActionResult SaveGroupDrug(List<GroupDrugInfo> items, string gpCode)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveGroupDrug(items, gpCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }

        public ActionResult GroupDrugAllDrugList(string gpCode)
        {
            LayData ht = new LayData("0", "获取成功");
            try
            {
                ht.data = _service.GroupDrugAllDrugList(gpCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult GroupDrugChoseDrugList(string gpCode)
        {
            LayData ht = new LayData("0", "获取成功");
            try
            {
                ht.data = _service.GroupDrugChoseDrugList(gpCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult SaveGP(GroupPurchase item, string type)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveGroupPurchase(item, type == "add");
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }



        public ActionResult ImportDrug(string gpCode)
        {
            LayData data = new LayData("0", "导入成功");
            try
            {
                var files = Request.Files;

                if (files.Count > 0)
                {
                    var filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upload", files[0].FileName);
                    if (files[0].ContentLength > 0)
                    {
                        files[0].SaveAs(filePath);
                        DataTable dt = ExcelHelper.ExcelToDataTable(filePath);
                        _service.ImportDrug(dt, gpCode);
                    }
                }
                else
                {
                    data.code = "2";
                    data.msg = "无上传文件";
                }
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }

        public ActionResult ImportTemplet()
        {
            DataTable dt = _service.ImportTemplet();
            string filename = ExcelHelper.ExportToExcel(dt, "集采药品维护模板");
            return File(filename, "application/vnd.ms-excel", "集采药品维护模板.xls");
        }
        public ActionResult ExportDrug(string gpCode, string name)
        {
            if (string.IsNullOrEmpty(name))
                name = "集采药品维护";
            DataTable dt = _service.ExportDrug(gpCode);
            string filename = ExcelHelper.ExportToExcel(dt, name);
            return File(filename, "application/vnd.ms-excel", name + ".xls");
        }

        public ActionResult DelGP(string gpCode)
        {
            LayData data = new LayData("0", "删除成功");
            try
            {
                _service.DelGroupPurchase(gpCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }


        public ActionResult EnabledGP(string gpCode)
        {
            LayData data = new LayData("0", "启用成功");
            try
            {
                _service.EnabledGroupPurchase(gpCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }

        //科室对照
        public ActionResult SaveCompare()
        {
            string gpCode = Request["gpCode"];
            string bs = Request["bs"];
            string[] baseArray = bs.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveCompare(gpCode, baseArray);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }
    }
}